//
//  DVLocationTableViewController.m
//  QuickCure
//
//  Created by 松鼠君 on 9/13/16.
//  Copyright © 2016 haoge. All rights reserved.
//

#import "DVLocationTableViewController.h"
#import "ViewController.h"
#import "AFNetworking.h"
#import "DVProvincesModel.h"
#import "HomeViewController.h"




typedef  NS_ENUM (NSInteger, identity){
    
    KidentityMain,KidentitySub
    
};

@interface DVLocationTableViewController ()
@property(nonatomic,strong)NSArray* provincesList;
@property(nonatomic,assign)identity  kID;
@property(nonatomic,assign)NSInteger rowNum;
@end

static NSString* reusedID = @"cell ";



@implementation DVLocationTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
    
    
}

-(NSArray*)provincesList{
    
    if (! _provincesList) {
        
        return  [DVProvincesModel arrayForProvinces];
    }
    
    return _provincesList;
    
}

-(void)setupUI{
    
    
    self.navigationItem.title = @"请选择当前省份";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"home_nav_button_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backToHome)];
    
    self.navigationItem.leftBarButtonItem.image  =  [self.navigationItem.leftBarButtonItem.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    

       
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:reusedID];
    
    
    
    
    
    
    
}


-(void)backToHome{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
   
    DVProvincesModel *model = self.provincesList[self.rowNum];
    
    NSInteger count = self.kID == KidentityMain? self.provincesList.count:model.cities.count;
    
    return count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DVProvincesModel* model = self.provincesList[indexPath.row];

    DVProvincesModel* model2 = self.provincesList[self.rowNum];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedID   forIndexPath:indexPath];
    
    cell.textLabel.text = self.kID == KidentityMain?model.name:model2.cities[indexPath.row];
    
    return cell;
}


- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.kID) {
        
        NSString* cityName =   [tableView cellForRowAtIndexPath:indexPath].textLabel.text ;
        
        HomeViewController* homeViewController = self.navigationController.viewControllers.firstObject ;
        
        [homeViewController  reloadWeatherViewData:cityName];

        
        [self.navigationController popToRootViewControllerAnimated:YES];
        
        
        return  nil;
        
    }
    
    DVLocationTableViewController* locationTableViewController = [DVLocationTableViewController new];
    
    
    locationTableViewController.rowNum = indexPath.row;
    
    locationTableViewController.kID = 1;


    [self.navigationController pushViewController:locationTableViewController animated:YES];
    
    
    return nil;
    
}




/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
